package io.github.talelin.latticy.service;

import io.github.talelin.autoconfigure.exception.HttpException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.dto.CollectMarkDTO;
import io.github.talelin.latticy.entity.SendCollect;
import io.github.talelin.latticy.repository.SendCollectRepository;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.SendCollectFlowerVO;
import io.github.talelin.latticy.vo.UnifyResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CollectFlowerService
{
    @Autowired
    private SendCollectRepository sendCollectRepository;

    @Autowired
    private SendCollectService sendCollectService;

    @Autowired
    private MarkImageService markImageService;

    public SendCollectFlowerVO getCollectFlower()
    {
        SendCollect sendCollect = sendCollectRepository.findFirstByCollectUserIdAndStatusAndDeleteTimeIsNullOrderByCreateTimeDesc(UserUtil.getUserId(), 0);
        if (sendCollect == null) {
            throw new HttpException(0, 200);
        }
        return new SendCollectFlowerVO(sendCollect);
    }

    public void create(String mobile, Long userId)
    {
        List<SendCollect> sendCollectList = sendCollectRepository.findIdByMobileAndCollectUserId(mobile);
        if (sendCollectList.size() > 0) {
            sendCollectList.forEach(sc -> {
                sc.setCollectUserId(userId);
            });
            sendCollectRepository.saveAll(sendCollectList);
        }
    }

    public PageResponseVO getFlowers(int page, int size)
    {
        Pageable pageable = PageRequest.of(page, size);
        Page<SendCollect> pager = sendCollectRepository.findByCollectUserIdAndDeleteTimeIsNullOrderByCreateTimeDesc(UserUtil.getUserId(), pageable);
        List<SendCollectFlowerVO> items = SendCollectFlowerVO.getLists(pager.getContent());
        return new PageResponseVO<>(pager.getTotalElements(), items, pager.getNumber(), pager.getSize());
    }

    public void delete(Long id)
    {
        SendCollect sendCollect = sendCollectService.get(id);
        sendCollect.setDeleteTime(new Date());
        sendCollectRepository.save(sendCollect);
    }

    @Transactional
    public void updateFile(Long id, CollectMarkDTO dto)
    {
        SendCollect sendCollect = sendCollectService.get(id);
        sendCollect.setCollectType(dto.getType());
        if (dto.getType() == 1) {
            //照片
            sendCollect.setFile(null);
            markImageService.create(id, dto.getFile());
        } else {
            //视频，语音
            sendCollect.setFile(dto.getFile().get(0));
        }
        sendCollectRepository.save(sendCollect);
    }
}
